//author:kakake
({
    data: {
        resultdata: {},
        dbConn: Program.data.dbConn,
        WorkId: Program.data.WorkId,
        requestBody: Program.data.requestBody,
    },

    //实现增删改查示例API控制器
    main: function (funname) {
        var self = this;
        //查询
        if (funname == "Wallet.getaccountlist") {
            var page = PublicFunction.RequestQuery("page", 1);
            var perPage = PublicFunction.RequestQuery("limit", 10);
            var accountNo = PublicFunction.RequestQuery("accountNo", "");
            var usertype = PublicFunction.RequestQuery("usertype", 0);
            var datagrid = self.getaccountlist(page, perPage, accountNo, usertype);
            self.data.resultdata.data = datagrid.data;
            self.data.resultdata.count = datagrid.count;
        } else if (funname == "Wallet.frozen") {
            var accountId = PublicFunction.RequestQuery("accountId", "");
            var status = PublicFunction.RequestQuery("status", "");
            PublicFunction.TrackLog("accountId", 0, accountId);
            PublicFunction.TrackLog("status", 0, status);
            var result = self.frozen(accountId, status);
            if (result > 0) {
                self.data.resultdata.code = 0;
                self.data.resultdata.msg = "操作成功";
            } else {
                throw '操作失败';
            }
        } else if (funname == "Wallet.getsharelist") {
            var page = PublicFunction.RequestQuery("page", 1);
            var perPage = PublicFunction.RequestQuery("limit", 10);
            var accountNo = PublicFunction.RequestQuery("accountNo", "");
            var datagrid = self.getsharelist(page, perPage, accountNo);
            self.data.resultdata.data = datagrid.data;
            self.data.resultdata.count = datagrid.count;
        }
        else if (funname == "Wallet.gettradelist") {
            var page = PublicFunction.RequestQuery("page", 1);
            var perPage = PublicFunction.RequestQuery("limit", 10);
            var orderNo = PublicFunction.RequestQuery("orderNo", "");
            var financialBalance = PublicFunction.RequestQuery("financialBalance", "");
            var datagrid = self.gettradelist(page, perPage, orderNo, financialBalance);
            self.data.resultdata.data = datagrid.data;
            self.data.resultdata.count = datagrid.count;
        }
        return self.data.resultdata;
    },
    getaccountlist: function (page, limit, accountNo, usertype) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "AccountId";
        pageInfo.page = page;
        pageInfo.perPage = limit;
        pageInfo.totalRecord = 0;
        pageInfo.totalPage = 0;
        var strsql = "SELECT * from WT_Account a WHERE a.WorkId=? ";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        if ((accountNo || "") != "") {
            strsql += " and (a.AccountNo Like ? or a.Name Like ?) ";
            dbparams.add('%' + accountNo + '%');
            dbparams.add('%' + accountNo + '%');
        }
        if (usertype > -1) {
            strsql += " and a.UserType =  ?";
            dbparams.add(usertype);
        }
        var data = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
        return { data: data, count: pageInfo.totalRecord };
    },
    frozen: function (accountId, status) {
        var self = this;
        var strsql = "update WT_Account set FrozenStatus=? WHERE WorkId=? and AccountId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(status);
        dbparams.add(self.data.WorkId);
        dbparams.add(accountId);
        return self.data.dbConn.executeUpdate(strsql, dbparams);
    },
    getsharelist: function (page, limit, accountNo) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "CreateTime desc";
        pageInfo.page = page;
        pageInfo.perPage = limit;
        pageInfo.totalRecord = 0;
        pageInfo.totalPage = 0;
        var strsql = "select a.*,b.RuleType,b.ProfitValue,d.Name,d.IDCard from CL_ShareRecord a\
        inner join CL_ShareRecordRule b\
        on a.RecordId=b.RecordId \
        inner join WT_Account d\
        on a.AccountNo=d.AccountNo where a.WorkId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        if ((accountNo || "") != "") {
            strsql += " and (a.AccountNo Like ? or d.Name Like ?)  ";
            dbparams.add('%' + accountNo + '%');
            dbparams.add('%' + accountNo + '%');
        }
        var data = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
        return { data: data, count: pageInfo.totalRecord };
    },
    gettradelist(page, limit, orderNo, financialBalance) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "EndTime desc";
        pageInfo.page = page;
        pageInfo.perPage = limit;
        pageInfo.totalRecord = 0;
        pageInfo.totalPage = 0;
        var strsql = "select a.*,b.Name,c.ShareNo from TR_TradeRecord a inner join Wt_Account b on a.UserId=b.UserId left join Cl_ShareRecord c on a.OrderNo=c.OrderNo where a.WorkId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        if ((orderNo || "") != "") {
            strsql += " and a.OrderNo Like ? ";
            dbparams.add('%' + orderNo + '%');
        }
        if (financialBalance > -1) {
            strsql += " and a.FinancialBalance = ? ";
            dbparams.add(financialBalance);
        }
        var data = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
        return { data: data, count: pageInfo.totalRecord };
    }
})